package sort;

public class LinkTable {
    public static void main(String[] args) {
//        System.out.println(getValue(10));
        reverse();
    }

    static void reverse() {
        Node<Integer> header = null;
        Node<Integer> end = null;

        Node<Integer> temp;
        //添加元素
        for (int i = 0; i < 10; i++) {
            if (i == 0) {
                header = new Node<>(i);
                end = header;
            } else {
                temp = new Node<>(i);
                end.next = temp;
                end = temp;
                temp = null;
            }
        }

        Node<Integer> result = null;
        Node<Integer> next = null;

        while (header != null) {
            next = header.next;

            header.next = result;
            result = header;

            header = next;

        }

        Node<Integer> node = result;
        do {
            System.out.println(node.value);
        } while ((node = node.next) != null);
    }

    static int getValue(int index) {
        Node<Integer> header = null;
        Node<Integer> end = null;
        //添加元素
        Node<Integer> tempNode;
        for (int i = 0; i < 10; i++) {
            if (i == 0) {
                header = new Node<>(i);
                end = header;
            } else {
                tempNode = new Node<>(i);
                end.next = tempNode;
                end = tempNode;
                tempNode = null;
            }
        }
        if (index < 1 || index > 10) {
            throw new RuntimeException("index不合法");
        }
        //查找元素
        Node<Integer> node1 = header;
        Node<Integer> node2 = header;
        int i = 0;
        do {
            if (i < index) {
                i++;
            } else {
                node2 = node2.next;
            }
        } while ((node1 = node1.next) != null);


        return node2.value;
    }
}


class Node<T> {
    public Node(T value) {
        this.value = value;
    }

    T value;
    Node<T> next;
}
